﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MDK.Model.Models;
using DevExpress.XtraEditors.Repository;
using MDK.Common;
using Newtonsoft.Json;

namespace MDK.Winform
{
    public partial class ucSystemSet : XtraUserControl
    {
        public ucSystemSet()
        {
            InitializeComponent();
        }

        private void ucSystemSet_Load(object sender, EventArgs e)
        {
            InitSet();
        }

        public void InitSet()
        {
            var key = txtKey.Text.Trim();
            string jsonWhere = "";
            if (!string.IsNullOrEmpty(key))
            {
                PropertyHelper property = new PropertyHelper();
                property.Addproperty(CompareType.Contains, "SysKey", key);
                jsonWhere = JsonConvert.SerializeObject(property.propertyList);
            }
            
            string url = string.Format("Systemsetting/FindList?jsonWhere={0}&orderPropertyName={1}&isAsc=false", jsonWhere, "SysKey");
            var list = WebAPIHelper.Get<List<T_Systemsetting>>(url);
            gdSystemSet.DataSource = list;
            gvSystemSet.RefreshData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSysConfig config = new frmSysConfig();
            if (config.ShowDialog() == DialogResult.OK)
            {
                FastMessageBox.Show("系统设置新增成功!");
                InitSet();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            InitSet();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperLinkEdit_Click(object sender, EventArgs e)
        {
            var sysId = gvSystemSet.GetFocusedRowCellValue("Id")?.ToString();
            if (!string.IsNullOrEmpty(sysId))
            {
                frmSysConfig config = new frmSysConfig(sysId);
                if (config.ShowDialog() == DialogResult.OK)
                {
                    FastMessageBox.Show("系统设置修改成功!");
                    InitSet();
                }
            }
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperLinkENABLED_Click(object sender, EventArgs e)
        {
            var sysId = gvSystemSet.GetFocusedRowCellValue("Id")?.ToString();
            if (!string.IsNullOrEmpty(sysId))
            {
                PropertyHelper jsonWhere = new PropertyHelper();
                jsonWhere.Addproperty(CompareType.Equal, "Id", sysId);
                string url = string.Format("Systemsetting/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
                var set = WebAPIHelper.Get<T_Systemsetting>(url);
                if (set == null)
                {
                    FastMessageBox.Error("无法获取配置信息！");
                }
                else
                {
                    if (FastMessageBox.Question("是否禁用此配置？") == DialogResult.OK)
                    {
                        set.Is_Enable = false;
                        WebAPIHelper.Post<T_Systemsetting>("Systemsetting/Modify", set);
                        InitSet();
                    }
                }
            }
        }


        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperLinkNoENABLED_Click(object sender, EventArgs e)
        {
            var sysId = gvSystemSet.GetFocusedRowCellValue("Id")?.ToString();
            if (!string.IsNullOrEmpty(sysId))
            {
                PropertyHelper jsonWhere = new PropertyHelper();
                jsonWhere.Addproperty(CompareType.Equal, "Id", sysId);
                string url = string.Format("Systemsetting/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
                var set = WebAPIHelper.Get<T_Systemsetting>(url);
                if (set == null)
                {
                    FastMessageBox.Error("无法获取配置信息！");
                }
                else
                {
                    if (FastMessageBox.Question("是否启用此配置？") == DialogResult.OK)
                    {
                        set.Is_Enable = true;
                        WebAPIHelper.Post<T_Systemsetting>("Systemsetting/Modify", set);
                        InitSet();
                    }
                }
            }
        }

        /// <summary>
        /// 行按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvSystemSet_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.RepositoryItem == null) return;
            RepositoryItemHyperLinkEdit repositoryItem = e.RepositoryItem as RepositoryItemHyperLinkEdit;
            if (repositoryItem == null) return;
            if (gvSystemSet.GetRowCellValue(e.RowHandle, "Is_Enable") == null) return;
            string is_enabled_string = gvSystemSet.GetRowCellValue(e.RowHandle, "Is_Enable").ToString();
            if (is_enabled_string == "True")
            {
                if (e.Column.Name == cancelcolumn.Name)
                    e.RepositoryItem = HyperLinkENABLED;
            }
            if (is_enabled_string == "False")
            {
                if (e.Column.Name == cancelcolumn.Name)
                    e.RepositoryItem = HyperLinkNoENABLED;
            }
        }
    }
}
